package com.lingo.a2b.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingo.a2b.mapper.ConnectFieldRelativeMapper;
import com.lingo.a2b.model.entity.ConnectFieldRelative;
import com.lingo.a2b.model.vo.ConnectFieldRelativeSaveVo;
import com.lingo.a2b.service.ConnectFieldRelativeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lingo
 * @since 2020-05-21
 */
@Service
public class ConnectFieldRelativeServiceImpl extends ServiceImpl<ConnectFieldRelativeMapper, ConnectFieldRelative> implements ConnectFieldRelativeService {


    @Override
    public List<ConnectFieldRelative> listByProjectId(Integer fprojectId) {
        QueryWrapper<ConnectFieldRelative> fieldRelativeQueryWrapper = new QueryWrapper<>();
        fieldRelativeQueryWrapper.lambda()
                .eq(ConnectFieldRelative::getFprojectId, fprojectId)
                .eq(ConnectFieldRelative::getFenable,true)
                .orderByAsc(ConnectFieldRelative::getFsrcFieldName);
        return this.list(fieldRelativeQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveOrUpdate(ConnectFieldRelativeSaveVo saveVo) {
        Integer fprojectId = saveVo.getFprojectId();
        QueryWrapper<ConnectFieldRelative> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ConnectFieldRelative::getFprojectId,fprojectId);
        this.remove(queryWrapper);
        if (CollectionUtils.isEmpty(saveVo.getFieldList())){
            return true;
        }
        saveVo.getFieldList().forEach(field -> field.setFprojectId(fprojectId));
        return saveOrUpdateBatch(saveVo.getFieldList());
    }


}
